$(function() {
    $( "#employee" ).autocomplete({
      source: function( request, response ) {
    	  ajaxAutocompleteSource(request, response);
      },
      minLength: 2,
      select: function( event, ui ) {
    	  ajaxxAutocompleteSelect(event, ui);
      },
      open: function() {
        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
      },
      close: function() {
        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
        $( "#employee" ).val("");
      }
    });
  });


function appendSubjectRow( row ) {
	$('#enterpriseEmployeesAndPartnersTable > tbody:last').append(row);
  }


function ajaxAutocompleteSource(request, response){
	 $.ajax({
         url: "http://imbi.ld.ttu.ee:7500/t104236_subjektid_projekt/?mode=person&event=ajax_person&ajaxTask=get_person_by_letter_overlap",
         dataType: "json",
         data: {
           letters: request.term
         },
         success: function( data ) {
             response( $.map( data, function( item ) {
               return {
                 label: item.first_name+' '+item.last_name,
                 value: item.person 
               }
             }));
           }
       });
}


function ajaxxAutocompleteSelect(event, ui){
	ajaxSavePersonAsEmployee(ui.item.value, ui.item.label);
}


function ajaxSavePersonAsEmployee(personId, labelValue){
	$.ajax({
        url: "http://imbi.ld.ttu.ee:7500/t104236_subjektid_projekt/?mode=employee&event=ajax_employee&ajaxTask=save_person_as_employee",
        dataType: "html",
        data: {
          enterpriseId: $("#enterpriseId").val(),
          personId: personId,
        },
        success: function( data ) {
        	if(data!=1){
        		alert(data);
        	}else{
        		
        		var redirectUrl = "'"+'?mode=employee&event=roles&personId='+personId+'&enterpriseId='+$("#enterpriseId").val()+"'";
        		appendSubjectRow(	"<tr id='personTableRow_id_"+personId+"'>" +
										"<td style='width:200px;'>"+labelValue+"</td>" +
										"<td>" +
											'<input type="button" onclick="window.location = '+redirectUrl+'"  value="Rollid" />' +
											"<input type='button' id='delete_"+personId+"' onclick='ajaxDeleteEmployeeFromEnterpriseVer2("+personId+", "+$("#enterpriseId").val()+")'  value='Kustuta' />" +
										"</td>" +
									"</tr>"
								);	
        	}
         }
      }); 
}

function ajaxDeleteEmployeeFromEnterprise(employeeId){
	$('#employeeTableRow_id_'+employeeId).remove();
	$.ajax({
        url: "http://imbi.ld.ttu.ee:7500/t104236_subjektid_projekt/?mode=employee&event=ajax_employee&ajaxTask=delete_employee",
        dataType: "html",
        data: {
        	employeeId: employeeId,
        },
        success: function( data ) {}
      }); 
}

function ajaxDeleteEmployeeFromEnterpriseVer2(personId, enterpriseId){
	$('#personTableRow_id_'+personId).remove();
	$.ajax({
        url: "http://imbi.ld.ttu.ee:7500/t104236_subjektid_projekt/?mode=employee&event=ajax_employee&ajaxTask=delete_employee",
        dataType: "html",
        data: {
        	personId: personId,
        	enterpriseId: enterpriseId
        },
        success: function( data ) {}
      }); 
}



/*function ajaxGetRolesSelectionForPerson(personId){
	$.ajax({
        url: "http://imbi.ld.ttu.ee:7500/t104236_subjektid_projekt/?mode=employee&event=ajax_employee&ajaxTask=get_roles_for_person",
        dataType: "html",
        data: {
          personId: personId
        },
        success: function( data ) {
        	$("#roleSelDiv_id_"+personId).html(data);
         }
      });
}*/

/*function ajaxSaveEnterpriseSubjectRelation(personId){
	var role = $("#roleSelectFor_id_"+personId).val();
	
	$.ajax({
        url: "http://imbi.ld.ttu.ee:7500/t104236_subjektid_projekt/?mode=employee&event=ajax_employee&ajaxTask=save_enterprise_subject_relation",
        dataType: "html",
        data: {
          enterpriseId: $("#enterpriseId").val(),
          personId: personId,
          role: role
        },
        success: function( data ) {
        	$("#roleSelDiv_id_"+personId).html(data);
         }
      }); 
	
} */